<%--
  Created by IntelliJ IDEA.
  User: 乐乐
  Date: 2021/7/10
  Time: 11:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>Title</title>
    <%String path =request.getContextPath();%>
    <script type="text/javascript" src="<%=path%>/js/jquery-2.1.1.min.js"></script>

    <script type="text/javascript">

        $(function(){
            x();
        })
        function x() {
            $ajax({
                url :"<%=path%>/to/getInfo",//请求地址
                type: get,
                data : $("#getForm").serialize(),
                dataType: "json",
                async :true,
                success :function (page){
                    alert("这里是成功函数")
            },
                error :function (page) {
                    alert("这里是失败函数")
                }
            })
        }
        function x(){
            $.ajax({
                url :"<%=path%>/to/getInfo",//请求地址
                type : "get",//请求方式
                data:$("#getForm").serialize(),
                dataType : "json",
                async :true,
                success : function(page){
                    $("#pageNum").val(page.pageNum);
                    $("#totalPage").val(page.pages);
                    $("#spanid").text(page.pageNum);
                    $("#spanid2").text(page.pages);
                    var result = page.list;
                    var table = "";
                    table +="<table width='50%' border ='1'>";
                    table +="<tr>";
                    table += "<th>Id</th>";
                    table += "<th>商品名称</th>";
                    table += "<th>商品价格</th>";
                    table += "<th>商品类型</th>";
                    table += "<th>上架时间</th>";
                    table += "<th>操作</th>";
                    table += "</tr>";
                    for (var i = 0; i < result.length; i++) {
                        table += "<tr>";
                        table += "<td>" + result[i].pid +"</td>";
                        table += "<td>" + result[i].pname +"</td>";
                        table += "<td>" + result[i].pprice +"</td>";
                        table += "<td>" + result[i].ttype +"</td>";
                        table += "<td>" + result[i].pdate +"</td>";
                        table += "<td>" + result[i].pdate +"</td>";
                        table += "<td>" + result[i].pdate +"</td>";
                        table += "</tr>";
                    }
                    table += "</table>";
                    $("#showdiv").html(table);
                },
                error : function(page){
                    alert("网网络异常,请刷新");
                }
            })
        }
        function topage(i){
            var pageNum = parseInt($("#pageNum").val());
            var totalPage = parseInt($("#totalPage").val());
            if (i==1){
                if (pageNum<=1){
                    alert("别点了,到头了")
                    return;
                }
                $("#pageNum").val(pageNum-1);
            } else if(i==2){
                if(pageNum>=totalPage){
                    alert("别点了,到头了")
                    return;
                }
                $("#pageNum").val(pageNum+1);
            }
            x();
        }


        function qx(){
            var arr = document.getElementsByName("che");
            for (let i = 0; i < arr.length; i++) {
                arr[i].checked = !arr[i].checked;
            }
        }
        //批量删除
        function deleteS(){
            var arr2 = new Array();
            var arr = document.getElementsByName("che");
            for (let i = 0; i < arr.length; i++) {
                if(arr[i].checked){
                    arr2.push(arr[i].value);
                }
            }
            location.href = "<%=path%>/to/deleteById?ids="+arr2;
        }
        //增加
        function insertS(){
            location.href = "<%=path%>/to/add";
        }
    </script>
</head>
<body>
<form id=getForm>
        品牌名:<input type="text" name="pname">
        生产日期始:<input type="date" name="pdate">
        生产日期至:<input type="date" name="pdate2">
        <input type="button" value="搜索" onclick="x()">
        <input type="button" value="删除" onclick="deleteS()">
        <input type="button" value="增加信息" onclick="insertS()">
        <hr>
        <div id="showdiv"></div>
        <input type="hidden" id="pageNum" name="pageNum">
        <input type="hidden" id="totalPage">
        <a href="javascript:topage(1)">上一页</a>
        <a href="javascript:topage(2)">下一页</a>
        第<span id="spanid"></span>页
        共<span id="spanid2"></span>页
    </form>
</body>
</html>
